
/**
** \file       until.c
**
** \brief      this function parse the until rule
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse, De
** Olivera Guillaume, and Nguyen Viet Dung
**
** \version    0.8
**
** \date       26 Novembre 2010
**
*/

#include "rules.h"

static void             *my_free(int                    n,
                                 s_ruleuntil            *until)
{
  if (n == 1)
  {
    free(until->in_until);
    until->in_until = NULL;
  }

  if (n == 2)
  {
    free(until->in_do);
    until->in_do = NULL;
  }

  return (NULL);
}

s_ruleuntil             *rule_until(s_ruleuntil         *until)
{
  if (g_data->lexer == NULL)
    return (NULL);

  if (strcmp(g_data->lexer->token->id, "until") == 0)
  {
    g_data->lexer = g_data->lexer->next;

    if (g_data->lexer == NULL)
      return (NULL);

    until->in_until = calloc(1, sizeof (s_cmpdlist));

    if (rule_compound_list(until->in_until) == NULL)
      return (my_free(1, until));

    if (g_data->lexer == NULL)
      return (my_free(1, until));

    until->in_do = calloc(1, sizeof (s_dogroup));

    if (rule_do_group(until->in_do) == NULL)
      return (my_free(2, until));

    return (until);
  }

  return (NULL);
}
